package top.xylweb.workflow.service;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.xylweb.workflow.dao.ReAttendMapper;
import top.xylweb.workflow.entity.ReAttend;

import java.util.*;

@Service("reAttendServiceImpl")
public class ReAttendServiceImpl implements ReAttendService{

    private static final java.lang.String RE_ATTEND_FLOW_ID = "re_attend";

    /**
     * 补签流程状态
     */
    private static final Byte RE_ATTEND_STATUS_ONGOING =1;
    private static final Byte RE_ATTEND_STATUS_PSSS =2;
    private static final Byte RE_ATTEND_STATUS_REFUSE =3;
    /**
     *
     */
    private static final Byte ATTEND_STATUS_NORMAL =1 ;
    /**
     * 流程下一步处理人
     */
    private static final String NEXT_HANDLER = "next_handler";
    /**
     * 任务关联补签数据键
     */
    private static final String RE_ATTEND_SIGN = "re_attend";

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private ReAttendMapper reAttendMapper;


    @Override
    @Transactional
    public void startReAttendFlow(ReAttend reAttend) {
        //从公司组织架构中 ，查询到此人的上级领导用户名
        reAttend.setCurrentHandler("lingdao");
        reAttend.setStatus(RE_ATTEND_STATUS_ONGOING);
        //插入数据库补签表
        reAttend.setAttendDate(new Date());
        reAttendMapper.insertSelective(reAttend);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put(RE_ATTEND_SIGN,reAttend.getId());
        map.put(NEXT_HANDLER,reAttend.getCurrentHandler());
        //启动补签流程实例
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(RE_ATTEND_FLOW_ID,map);
        //提交用户补签任务
        Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
        taskService.complete(task.getId(),map);
    }

    @Override
    public List<ReAttend> listTasks(String userName) {
        List<ReAttend> reAttendList = new ArrayList<ReAttend>();
        List<Task> taskList = taskService.createTaskQuery().processVariableValueEquals(userName).list();
        //装换成页面实体
        if(CollectionUtils.isNotEmpty(taskList)){
            for(Task task:taskList){
                Map<String,Object> variable = taskService.getVariables(task.getId());
                ReAttend reAttend = new ReAttend();
                reAttend.setTaskId((String) variable.get(RE_ATTEND_SIGN));
                reAttendList.add(reAttend);
            }
        }
        return reAttendList;
    }

    @Override
    @Transactional
    public void approve(ReAttend reAttend) {
        Task task = taskService.createTaskQuery().taskId(reAttend.getTaskId()).singleResult();
        if(RE_ATTEND_STATUS_PSSS.toString().equals(reAttend.getApproveFlag())){
            //审批状态通过 修改补签数据状态
            reAttend.setStatus(RE_ATTEND_STATUS_PSSS);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
        }else if(RE_ATTEND_STATUS_REFUSE.toString().equals(reAttend.getApproveFlag())){
            reAttend.setStatus(RE_ATTEND_STATUS_REFUSE);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
        }
        taskService.complete(reAttend.getTaskId());
    }

    @Override
    public List<ReAttend> listReAttend(String username) {
        List<ReAttend> list =reAttendMapper.selectReAttendRecord(username);
        return list;
    }
}
